const { regClass } = Laya;
import { EViewType } from "../Data/DataType";
import { EventNames } from "../Data/EventNames";
import { GoodsLisBase } from "./GoodsLis.generated";

@regClass()
export class GoodsLis extends GoodsLisBase {
    private goodsId:number;
    private data: any;
    onEnable(): void {
        this.BtnExChange.on(Laya.Event.CLICK,this,this.onClickExChange)
    }
    onDestroy(): void {
        this.BtnExChange.off(Laya.Event.CLICK,this,this.onClickExChange)
    }
    onClickExChange(){
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        Laya.stage.event(EventNames.GAME_OPEN_VIEW,EViewType.ExChange)
        Laya.stage.event(EventNames.INIT_SHOP_VIEW,{need:this.Need.text,goodsName:this.GoodsName.text,img:this.BG.source,id:this.goodsId})
    }
    public setData(data:any){
        this.data = data;
        let txt = this.getChildByName("ImgBg").getChildByName("GoodsName") as Laya.Label;
        let needscore = this.getChildByName("ImgBg").getChildByName("HBox").getChildByName("Need") as Laya.Label;
        let goods = this.getChildByName("ImgBg").getChildByName("ImgBox").getChildByName("BG") as Laya.Image;
        let nonestate = this.getChildByName("ImgBg").getChildByName("ImgBox").getChildByName("None") as Laya.Image;
        goods.source = undefined
        goods.skin=undefined
        Laya.loader.load(data.img, Laya.Handler.create(this, (texture: Laya.Texture) => {
            if(this.data.img == data.img){
                let boxW = 268
                let boxH = 211
                let rate = boxW / boxH;
                let rate2 = texture.width / texture.height;
                if(rate > rate2){
                    goods.height = boxH
                    goods.width = boxH * rate2;
                }
                else{
                    goods.height = boxW / rate2
                    goods.width = boxW;
                }
                goods.skin=undefined
                goods.source = texture;
            }
        }), undefined, Laya.Loader.IMAGE)
        if(data.status==0)
        {
            nonestate.visible=false
        }
        else
        {
            nonestate.visible=true
        }
        txt.text = data.goodsName;
        needscore.text = data.need;
        this.goodsId=data.id;
    }
}